﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using FileDFS.Model;
using FileDFS.Model.Enum;
using System.Drawing;
using System.Drawing.Imaging;

namespace FileDFS.Utils
{
    public class ImageHelper
    {
        public static ImageHelper GetInstance()
        {
            return new ImageHelper();
        }

        #region 保存图片,并且直接生成了缩略图
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="sourceImage"></param>
        /// <param name="thumbnailModel"></param>
        /// <param name="currentPath"></param>
        /// <returns></returns>
        public  ThumbnailResult CutToThumbnail(Image sourceImage, ThumbnailModel thumbnailModel, Utils.PathConfig.PathDetail currentPath)
        {
            ThumbnailResult thumbnail = new ThumbnailResult();
            thumbnail.ThumbnailID = thumbnailModel.ThumbnailId;
            //生成文件名,带扩展名的
            string imageName = AlgorithmHelper.GetGuid() + "." + thumbnailModel.ThumbnailExtension.ToString();
            //设置返回ThumbnailResult的文件名和路径
            thumbnail.ThumbnailFullPath = PathHelper.CombineRelativePath(currentPath.Virtual_FullPath, imageName);

            #region 初始化上下文
            ImageZoomContext context = new ImageZoomContext()
                {
                    SourceImage = sourceImage,
                    Mode = thumbnailModel.Mode,
                    ZoomWidth = thumbnailModel.ThumbnailWidth,
                    ZoomHight = thumbnailModel.ThumbnailHeight
                }; 
            #endregion

            //获取相关参数
            context = GetSize(context);
            Image bitmap = GetZoomImage(context);
            bitmap.Save(Path.Combine(currentPath.Absolute_FullPath, imageName));
            return thumbnail;
        } 
        #endregion

        #region 根据生成图片的上下文生成缩略图
        /// <summary>
        /// 根据生成图片的上下文生成缩略图
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns>新图片 Image</returns>
        public Image GetZoomImage(ImageZoomContext context)
        {
            try
            {
                System.Drawing.Image bitmap = new System.Drawing.Bitmap(context.ZoomWidth, context.ZoomHight);
                //新建一个画板
                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
                {
                    //设置高质量插值法
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                    //设置高质量,低速度呈现平滑程度
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    //清空画布并以透明背景色填充
                    g.Clear(System.Drawing.Color.Transparent);
                    //在指定位置并且按指定大小绘制原图片的指定部分
                    //System.Drawing.Rectangle(_x, _y, OldWidth, OldHeigh)指定从元图片的哪里开始画，画多少
                    g.DrawImage(context.SourceImage, new System.Drawing.Rectangle(0, 0, context.Size.TargetWidth, context.Size.TargetHeight), new System.Drawing.Rectangle(context.Size.StartX, context.Size.StartY, context.Size.OriginalWidth, context.Size.OriginalHeight), System.Drawing.GraphicsUnit.Pixel);

                    return bitmap;
                }

            }
            catch (Exception ex)
            {
                Utils.TestLog.Logger(ex.Message);
                return null;
            }
        }
        #endregion

        #region 根据生成图片的上下文生成相关的缩略图参数
        /// <summary>
        /// 根据生成图片的上下文生成相关的缩略图参数
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns>带有参数的上下文</returns>
        public ImageZoomContext GetSize(ImageZoomContext context)
        {
            #region 初始化参数
            ThumbnailSize size = new ThumbnailSize()
               {
                   StartX = 0,
                   StartY = 0,
                   TargetWidth = context.ZoomWidth,
                   TargetHeight = context.ZoomHight,
                   OriginalWidth = context.SourceImage.Width,
                   OriginalHeight = context.SourceImage.Height
               };
            #endregion

            #region +根据所放的类型设置宽度，高度
            switch (context.Mode)
            {
                case ImageCutModeEnum.Specified:
                    break;
                case ImageCutModeEnum.AutoHeigh: size.TargetHeight = context.SourceImage.Height * context.ZoomWidth / context.ZoomWidth;  //指定宽度，高度按比例
                    break;
                case ImageCutModeEnum.AutoWidth: size.TargetWidth = context.SourceImage.Width * context.ZoomHight / context.ZoomHight; //指定高度，宽度按照比例变化
                    break;
                case ImageCutModeEnum.Auto:
                    //指定宽度和高度进行切割，但是不会变形
                    if ((double)context.SourceImage.Width / (double)context.SourceImage.Height > (double)size.TargetWidth / (double)size.TargetHeight)
                    {
                        //如果原图的  宽/高 比例比新图的 宽/高比例高大，那么高度的起始位置不会变化
                        size.OriginalHeight = context.SourceImage.Height;
                        size.OriginalWidth = context.SourceImage.Width * size.TargetWidth / size.TargetHeight;
                        size.StartX = (context.SourceImage.Width - size.OriginalWidth) / 2;
                    }
                    else
                    {
                        size.OriginalWidth = context.SourceImage.Width;
                        size.OriginalHeight = context.SourceImage.Width * context.ZoomHight / size.TargetWidth;
                        size.StartY = (context.SourceImage.Height - size.TargetHeight) / 2;
                    }
                    break;
                default:
                    break;
            }
            #endregion

            context.Size = size;

            return context;
        } 
        #endregion

        #region 将图片从Image转换成Stream
        public Stream GetStreamFromImage(Image image)
        {
            MemoryStream stream = new MemoryStream();
            image.Save(stream, ImageFormat.Bmp);
            
            return stream;

        }
        #endregion


    }
}
